import { doAction } from "@/utils/chrome";
import utilEventBus from "../utils/eventBus";
import {
  setDragConfigIsDomain,
  setDragConfigIsPage,
  getDragConfig
} from "./contenters/drag-config";

/**
 * content 给其他层调用的方法
 */
const contenters = {
  setDragConfigIsDomain,
  setDragConfigIsPage,
  getDragConfig
};

export const chromeOnMessage = function () {
  console.log("init message content");
  chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
    console.log("content.js 收到请求了", request);
    if (!contenters[request.action]) {
      utilEventBus.$emit(request.action, request.data);
      return;
    }
    doAction(contenters[request.action], request.data).then(sendResponse);
    return true;
  });
};
